package Demo_C;

import javax.swing.plaf.synth.SynthRadioButtonUI;

public class Demo_C {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //throw new RuntimeException(e);
                    //添加break之后，此时触发异常的时候也会结束循环
                    //在break之前，也可以添加其他代码逻辑，相当于善后
                    for (int i = 3; i >= 0; i--) {
                        System.out.println("结束倒计时" + i);
                    }

                    break;
                }
            }
        });

        //启动线程
        t.start();

        //主线程休眠 5 秒
        Thread.sleep(5000);

        //调用这个方法，就是把标志位 isInterrupted() 从false 改为 true
        t.interrupt();
    }
}
